package com.anjubao.parking.municipal.parkinglot.task;

import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingStatRecordMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingStatSlotMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingStatSlot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 车位定时统计任务
 * @author anjubao
 */
//@Component
public class SlotTask {

    @Autowired
    private ParkingStatRecordMapper parkingStatRecordMapper;
    @Autowired
    private ParkingStatSlotMapper parkingStatSlotMapper;

    /**
     * 每隔一小时统计一次车场车位数
     */
    @Scheduled(cron = "${task.cron}")
    public void refreshParkingSlot(){
            List<ParkingStatSlot> parkingSlots = parkingStatRecordMapper.getParkingSlot();
            for (ParkingStatSlot parkingSlot : parkingSlots) {
                Long id = U.id();
                //按照特定格式获取时间
                String nowTimeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime dateTime = LocalDateTime.parse(nowTimeString, df);
                parkingSlot.setStatTime(dateTime);
                parkingSlot.setId(id);
                parkingSlot.setCreateTime(LocalDateTime.now());
                parkingStatSlotMapper.insert(parkingSlot);
            }
    }
}
